<?php
class adminx extends CI_Controller {
	function index() {
		$this -> pub -> logp('访问首页', '1-2-3');
		$da = $this -> pub -> get_setting('首页', 'index');

		//统计
		$this -> db -> select('count(*)');
		$q = $this -> db -> get_where('member');
		$ls = $q -> row_array(0);
		$da['member'] = $ls['count(*)'];

		$this -> db -> select('count(*)');
		$this -> db -> where('wz_del', 1);
		$q = $this -> db -> get_where('wz');
		$ls = $q -> row_array(0);
		$da['wz'] = $ls['count(*)'];

		$this -> db -> select('count(*)');
		$this -> db -> where('sp_del', 1);
		$q = $this -> db -> get_where('sp');
		$ls = $q -> row_array(0);
		$da['sp'] = $ls['count(*)'];

		//$this -> db -> select('count(*)');
		//$q = $this -> db -> get_where('log');
		//$ls = $q -> row_array(0);
		$da['log'] = 0;

		$this -> db -> select('sum(numb)');
		$this -> db -> where('state in(1,2,3)');
		$q = $this -> db -> get_where('bill');
		$ls = $q -> row_array(0);
		$da['bill'] = $ls['sum(numb)'] ? $ls['sum(numb)'] : '0';

		//$this -> db -> select('count(*)');
		//$q = $this -> db -> get_where('wx');
		//$ls = $q -> row_array(0);
		$da['wx'] = '10W+';

		$this -> db -> select('sum(je)');
		$this -> db -> where('type', 1);
		$q = $this -> db -> get_where('jb');
		$ls = $q -> row_array(0);
		$da['je'] = $ls['sum(je)'] ? $ls['sum(je)'] : '0';
		if ($this -> config -> item('xt_en') == 'nwrq') {
			$da['je'] = sprintf("%.2f", $da['je'] / 10);
		}

		$this -> load -> view('adminx/index', $da);
	}

	function get_member_ewm() {
		$this -> pub -> logp('获取二维码', '1-2-3');
		$da['ewm'] = $this -> pub -> get_ewm($this -> input -> post('mid'), $this -> input -> post('type'));
		echo $da['ewm'];
	}

	function yl() {
		$this -> pub -> logp('预览', '1-2');
		if ($this -> input -> get('mid')) {
			$this -> db -> select('headimgurl,tj_mid');
			$q = $this -> db -> get_where('member', array('mid' => $this -> input -> get('mid')));
			$m = $q -> row_array(0);

			$da['head'] = $m['headimgurl'];
			$da['min'] = TRUE;
			$da['mid'] = $this -> input -> get('mid');
			$da['tj_mid'] = $m['tj_mid'];

			$this -> session -> set_userdata($da);
		}

		$da = $this -> pub -> get_setting('预览', 'Preview');
		$this -> load -> view('adminx/yl', $da);
	}

	function tj_sp() {
		$this -> pub -> logp('设置首页', '1-2');
		$in['tj_sp'] = $this -> input -> post('tj_sp');
		$this -> db -> update('setting', $in);
		echo json_encode(array('type' => 1));
	}

	function xj_tj() {
		$this -> pub -> logp('获取现金统计', '1-2');
		$da['numb'] = $this -> pub -> get_jb_tj($this -> input -> post('type'), $this -> input -> post('date1'), $this -> input -> post('date2'));
		echo json_encode($da);
	}

	function xj_dd() {
		$this -> pub -> logp('订单统计', '1-2');
		if ($this -> input -> post('date1')) {
			$date1 = strtotime($this -> input -> post('date1'));
			$this -> db -> where('mktime >', $date1);
		}

		if ($this -> input -> post('date2')) {
			$date2 = strtotime($this -> input -> post('date2') . ' 23:59:59');
			$this -> db -> where('mktime <', $date2);
		}

		$this -> db -> select('sum(numb)');
		$this -> db -> where('state in(1,2,3)');
		$q = $this -> db -> get_where('bill');
		$ls = $q -> row_array(0);
		$da['numb'] = $ls['sum(numb)'];
		echo json_encode($da);
	}

	function tj_xn() {
		$this -> pub -> logp('虚拟订单统计', '1-2');
		if ($this -> input -> post('date1')) {
			$date1 = strtotime($this -> input -> post('date1'));
			$this -> db -> where('mktime >', $date1);
		}

		if ($this -> input -> post('date2')) {
			$date2 = strtotime($this -> input -> post('date2') . ' 23:59:59');
			$this -> db -> where('mktime <', $date2);
		}

		$this -> db -> select('sum(je)');
		$this -> db -> where('state in(1,2,3)');
		$this -> db -> where('thfs in(3,4)');
		$q = $this -> db -> get_where('bill');
		$ls = $q -> row_array(0);
		$da['je'] = $ls['sum(je)'];
		echo json_encode($da);
	}

	function tj_wl() {
		$this -> pub -> logp('物流订单统计', '1-2');
		if ($this -> input -> post('date1')) {
			$date1 = strtotime($this -> input -> post('date1'));
			$this -> db -> where('mktime >', $date1);
		}

		if ($this -> input -> post('date2')) {
			$date2 = strtotime($this -> input -> post('date2') . ' 23:59:59');
			$this -> db -> where('mktime <', $date2);
		}

		$this -> db -> select('sum(je)');
		$this -> db -> where('state in(1,2,3)');
		$this -> db -> where('thfs', 1);
		$q = $this -> db -> get_where('bill');
		$ls = $q -> row_array(0);
		$da['je'] = sprintf("%.0f", $ls['sum(je)']);
		echo json_encode($da);
	}

	//获取实时通知
	function get_shi() {
		if ($this -> session -> userdata('sin')) {
			$this -> pub -> logp('获取通知', '1-2-3');
			$this -> db -> select('count(*)');
			//设置权限
			if ($this -> session -> userdata('state') == 3) {
				$this -> db -> where('bill_id in(select bill_id from pan_bill_item where sp_id in(select sp_id from pan_sp where staff_id=' . $this -> session -> userdata('sid') . '))');
			}
			$q = $this -> db -> get_where('bill', array('state' => 1));
			$ls = $q -> row_array(0);
			$da['bill'] = $ls['count(*)'];

			//设置权限
			if ($this -> session -> userdata('state') == 3) {
				$da['wx'] = 0;
			} else {
				//$this -> db -> select('count(*)');
				//$q = $this -> db -> get_where('wx', array('state' => 1, 'MsgType' => 'text'));
				//$ls = $q -> row_array(0);
				$da['wx'] = '99+';
			}

			$this -> db -> select('pan_member.headimgurl,pan_bill.name,pan_bill.mktime,pan_bill.zz,pan_member.mid');
			$this -> db -> order_by('bill_id', 'desc');
			$this -> db -> limit(10);
			$this -> db -> join('member', 'pan_member.mid=pan_bill.mid');
			//设置权限
			if ($this -> session -> userdata('state') == 3) {
				$this -> db -> where('bill_id in(select bill_id from pan_bill_item where sp_id in(select sp_id from pan_sp where staff_id=' . $this -> session -> userdata('sid') . '))');
			}
			$q = $this -> db -> get_where('bill', array('pan_bill.state' => 1));
			$da['bills'] = '';
			foreach ($q->result_array() as $v) {
				$da['bills'] .= '<li>
									<a href="/iadmin/member/info/' . $v['mid'] . '">
									<span class="photo">
									<img src="' . $v['headimgurl'] . '" class="img-circle" alt="">
									</span>
									<span class="subject">
									<span class="from">
									' . $v['name'] . ' </span>
									<span class="time">' . date('Y-m-d H:i', $v['mktime']) . ' </span>
									</span>
									<span class="message">
									' . $v['zz'] . '  </span>
									</a>
								</li>';
			}

			$da['wxs'] = '';

			if ($this -> session -> userdata('state') != 3) {
				$this -> db -> order_by('wx_id', 'desc');
				$this -> db -> limit(10);
				$q = $this -> db -> get_where('wx', array('pan_wx.state' => 1, 'pan_wx.MsgType' => 'text'));
				foreach ($q->result_array() as $v) {
					$ls = unserialize($v['content']);
					$this -> db -> select('pan_member.headimgurl,pan_member.nickname,pan_member.mid');
					$qq = $this -> db -> get_where('member', array('openid' => $v['FromUserName']));
					$m = $qq -> row_array(0);
					$da['wxs'] .= '<li>
									<a href="/iadmin/member/info/' . $m['mid'] . '">
									<span class="photo">
									<img src="' . $m['headimgurl'] . '" class="img-circle" alt="">
									</span>
									<span class="subject">
									<span class="from">
									' . $m['nickname'] . ' </span>
									<span class="time">' . date('Y-m-d H:i', $v['CreateTime']) . ' </span>
									</span>
									<span class="message">
									' . $ls['Content'] . '  </span>
									</a>
								</li>';
				}
			}
			$da['type'] = 1;
		} else {
			$da['type'] = 2;
		}
		echo json_encode($da);
	}

	function tx($mid) {
		$this -> pub -> logp('查看会员信息', '1-2');
		$da = $this -> pub -> get_setting('会员信息', 'index');
		$q = $this -> db -> get_where('member', array('mid' => $mid));
		$da['m'] = $q -> row_array(0);

		//获取剩余
		$da['zj_sy'] = $this -> pub -> get_jb($mid);
		//总提现
		$this -> db -> where('mid', $mid);
		$this -> db -> where('type', -1);
		$this -> db -> where('state >', 0);
		$this -> db -> select('sum(je)');
		$q = $this -> db -> get_where('jb');
		$ls = $q -> row_array(0);
		$da['zj_tx'] = $ls['sum(je)'];
		//总收益
		$this -> db -> where('mid', $mid);
		$this -> db -> where('type in(2,4)');
		$this -> db -> where('state >', 0);
		$this -> db -> select('sum(je)');
		$q = $this -> db -> get_where('jb');
		$ls = $q -> row_array(0);
		$da['zj_zsy'] = $ls['sum(je)'];

		//获取他推荐的账单
		$q = $this -> db -> get_where('jb', array('mid' => $mid));
		$da['bill'] = $q -> result_array();

		//获取粉丝
		$this -> db -> select('nickname,headimgurl,subscribe_time');
		$q = $this -> db -> get_where('member', array('tj_mid' => $mid));
		$da['fs'] = $q -> result_array();

		$this -> load -> view('adminx/tx', $da);
	}

	function out() {
		$this -> session -> sess_destroy();
		$this -> session -> set_userdata('sin', FALSE);
		$this -> session -> set_userdata('state', 0);
		redirect('/iadmin/login');
	}

	function wx_hf() {
		$this -> pub -> logp('回复会员信息', '1-2');
		if ($this -> input -> post('content')) {
			$this -> load -> helper('string');
			$da['FromUserName'] = $this -> input -> post('idx');
			$da['ToUserName'] = 1;
			$da['CreateTime'] = mktime();
			$da['MsgType'] = 'text';
			$da['MsgId'] = random_string('md5');
			$co['Content'] = $this -> input -> post('content');
			$da['content'] = serialize($co);
			$this -> db -> insert('wx', $da);
			$this -> pub -> send_zd_text($this -> input -> post('idx'), $this -> input -> post('content'));
			$this -> db -> update('wx', array('state' => 2), array('FromUserName' => $this -> input -> post('idx')));
			echo json_encode(array('type' => 1));
		} else {
			echo json_encode(array('type' => 2));
		}
	}

	function cd() {
		$this -> load -> helper('url');
		$this -> pub -> logx('更新微信菜单');
		$access_token = $this -> pub -> access_token();
		if ($access_token['type']) {
			//获取按钮内容
			$data = '{"button":[';
			$this -> db -> where('sf in(5,2)');
			$this -> db -> order_by('sort', 'asc');
			$q = $this -> db -> get_where('fl', array('pid' => '0', 'fl_del' => 1));
			if ($q -> num_rows() > 3) {
				exit('按钮数量不可以大于3！');
			}
			$dhx1 = '';
			$dh1 = '';
			$gn = array('scancode_push', 'scancode_waitmsg', 'pic_sysphoto', 'pic_photo_or_album', 'pic_weixin', 'location_select');
			foreach ($q->result_array() as $v) {
				$data .= $dhx1 . '{';
				$data .= ' "name":"' . $v['name'] . '",';
				$this -> db -> where('sf in(5,2)');
				$this -> db -> order_by('sort', 'asc');
				$qq = $this -> db -> get_where('fl', array('pid' => $v['fl_id'], 'fl_del' => 1));
				if ($qq -> num_rows() > 0) {
					$data .= '"sub_button":[';
					$dhx = '';
					foreach ($qq->result_array() as $vv) {
						$data .= $dhx . '{';
						$data .= '"name":"' . $vv['name'] . '",';

						if ($vv['sf'] == 2) {
							$data .= '"type":"view",';
							$data .= '"url":"' . $vv['url'] . '"';
						} elseif (in_array($vv['url'], $gn)) {
							$data .= '"type":"' . $vv['url'] . '",';
							$data .= '"key":"rselfmenu_0_1"';
						} else {
							$data .= '"type":"click",';
							$data .= '"key":"' . $vv['url'] . '"';
						}

						$data .= '}';
						$dhx = ',';
					}
					$data .= ']';
				} else {
					if ($v['sf'] == 2) {
						$data .= '"type":"view",';
						$data .= '"url":"' . $v['url'] . '"';
					} else {
						$data .= '"type":"click",';
						$data .= '"key":"' . $v['url'] . '"';
					}
				}
				$data .= '}';
				$dhx1 = ',';
			}
			$data .= '] }';
			$ch = curl_init();
			curl_setopt($ch, CURLOPT_URL, "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=" . $access_token['access_token']);
			curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
			curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
			curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
			curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');
			curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
			curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
			curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
			curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
			$tmpInfo = curl_exec($ch);
			if (curl_errno($ch)) {
				echo 'Errno' . curl_error($ch);
			}
			curl_close($ch);
			echo '更新微信菜单成功，24小时内生效！';
			var_dump($tmpInfo);
		} else {
			echo 'wxAppId错误，更新微信按钮失败！';
		}
	}

	//发红包
	function send_hb() {
		$this -> db -> trans_start();
		$this -> pub -> logp('补发红包 ' . $this -> input -> post('idx'), '1-2');
		$mid = $this -> input -> post('mid');
		$idx = $this -> input -> post('idx');
		$q = $this -> db -> get_where('jb', array('id' => $idx));
		$ls = $q -> row_array(0);
		$type = $ls['type'];
		//在这里发红包
		if ($type == 4) {
			$da = $this -> pub -> send_yjhb($mid, $idx);
		}
		//这里发货款红包
		if ($type == 5) {
			$da = $this -> pub -> send_skhb($mid, $idx);
		}
		//分享红包
		if ($type == 6) {
			$da = $this -> pub -> send_fxhb($mid, $idx);
		}
		$this -> db -> trans_complete();
		echo json_encode($da);
	}

}
?>